IS 675 Lab3: Pretrained Networks¶

In [1]:
# Import rquired libraries
from torchvision import models
from torchvision import transforms
import torch
import torch.nn as nn
from PIL import Image
from google.colab import drive
In [2]:
# Mounting Google Drive
drive.mount('/content/drive')
Mounted at /content/drive

1. Obtaining a pretrained network for image recognition¶

The predefined models can be found in torchvision.models

We can take a look at the actual models: The capitalized names refer to Python classes that implement a number of popular models. They differ in their architecture—that is, in the arrangement of the operations occurring between the input and the output. The lowercase names are convenience functions that return models instantiated from those classes, sometimes with different parameter sets. For instance, resnet101 returns an instance of ResNet with 101 layers, resnet18 has 18 layers, and so on.

In [3]:
# List of pretrained models from torchvision
dir(models)
Out[3]:
['AlexNet',
 'AlexNet_Weights',
 'ConvNeXt',
 'ConvNeXt_Base_Weights',
 'ConvNeXt_Large_Weights',
 'ConvNeXt_Small_Weights',
 'ConvNeXt_Tiny_Weights',
 'DenseNet',
 'DenseNet121_Weights',
 'DenseNet161_Weights',
 'DenseNet169_Weights',
 'DenseNet201_Weights',
 'EfficientNet',
 'EfficientNet_B0_Weights',
 'EfficientNet_B1_Weights',
 'EfficientNet_B2_Weights',
 'EfficientNet_B3_Weights',
 'EfficientNet_B4_Weights',
 'EfficientNet_B5_Weights',
 'EfficientNet_B6_Weights',
 'EfficientNet_B7_Weights',
 'EfficientNet_V2_L_Weights',
 'EfficientNet_V2_M_Weights',
 'EfficientNet_V2_S_Weights',
 'GoogLeNet',
 'GoogLeNetOutputs',
 'GoogLeNet_Weights',
 'Inception3',
 'InceptionOutputs',
 'Inception_V3_Weights',
 'MNASNet',
 'MNASNet0_5_Weights',
 'MNASNet0_75_Weights',
 'MNASNet1_0_Weights',
 'MNASNet1_3_Weights',
 'MaxVit',
 'MaxVit_T_Weights',
 'MobileNetV2',
 'MobileNetV3',
 'MobileNet_V2_Weights',
 'MobileNet_V3_Large_Weights',
 'MobileNet_V3_Small_Weights',
 'RegNet',
 'RegNet_X_16GF_Weights',
 'RegNet_X_1_6GF_Weights',
 'RegNet_X_32GF_Weights',
 'RegNet_X_3_2GF_Weights',
 'RegNet_X_400MF_Weights',
 'RegNet_X_800MF_Weights',
 'RegNet_X_8GF_Weights',
 'RegNet_Y_128GF_Weights',
 'RegNet_Y_16GF_Weights',
 'RegNet_Y_1_6GF_Weights',
 'RegNet_Y_32GF_Weights',
 'RegNet_Y_3_2GF_Weights',
 'RegNet_Y_400MF_Weights',
 'RegNet_Y_800MF_Weights',
 'RegNet_Y_8GF_Weights',
 'ResNeXt101_32X8D_Weights',
 'ResNeXt101_64X4D_Weights',
 'ResNeXt50_32X4D_Weights',
 'ResNet',
 'ResNet101_Weights',
 'ResNet152_Weights',
 'ResNet18_Weights',
 'ResNet34_Weights',
 'ResNet50_Weights',
 'ShuffleNetV2',
 'ShuffleNet_V2_X0_5_Weights',
 'ShuffleNet_V2_X1_0_Weights',
 'ShuffleNet_V2_X1_5_Weights',
 'ShuffleNet_V2_X2_0_Weights',
 'SqueezeNet',
 'SqueezeNet1_0_Weights',
 'SqueezeNet1_1_Weights',
 'SwinTransformer',
 'Swin_B_Weights',
 'Swin_S_Weights',
 'Swin_T_Weights',
 'Swin_V2_B_Weights',
 'Swin_V2_S_Weights',
 'Swin_V2_T_Weights',
 'VGG',
 'VGG11_BN_Weights',
 'VGG11_Weights',
 'VGG13_BN_Weights',
 'VGG13_Weights',
 'VGG16_BN_Weights',
 'VGG16_Weights',
 'VGG19_BN_Weights',
 'VGG19_Weights',
 'ViT_B_16_Weights',
 'ViT_B_32_Weights',
 'ViT_H_14_Weights',
 'ViT_L_16_Weights',
 'ViT_L_32_Weights',
 'VisionTransformer',
 'Weights',
 'WeightsEnum',
 'Wide_ResNet101_2_Weights',
 'Wide_ResNet50_2_Weights',
 '_GoogLeNetOutputs',
 '_InceptionOutputs',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__',
 '_api',
 '_meta',
 '_utils',
 'alexnet',
 'convnext',
 'convnext_base',
 'convnext_large',
 'convnext_small',
 'convnext_tiny',
 'densenet',
 'densenet121',
 'densenet161',
 'densenet169',
 'densenet201',
 'detection',
 'efficientnet',
 'efficientnet_b0',
 'efficientnet_b1',
 'efficientnet_b2',
 'efficientnet_b3',
 'efficientnet_b4',
 'efficientnet_b5',
 'efficientnet_b6',
 'efficientnet_b7',
 'efficientnet_v2_l',
 'efficientnet_v2_m',
 'efficientnet_v2_s',
 'get_model',
 'get_model_builder',
 'get_model_weights',
 'get_weight',
 'googlenet',
 'inception',
 'inception_v3',
 'list_models',
 'maxvit',
 'maxvit_t',
 'mnasnet',
 'mnasnet0_5',
 'mnasnet0_75',
 'mnasnet1_0',
 'mnasnet1_3',
 'mobilenet',
 'mobilenet_v2',
 'mobilenet_v3_large',
 'mobilenet_v3_small',
 'mobilenetv2',
 'mobilenetv3',
 'optical_flow',
 'quantization',
 'regnet',
 'regnet_x_16gf',
 'regnet_x_1_6gf',
 'regnet_x_32gf',
 'regnet_x_3_2gf',
 'regnet_x_400mf',
 'regnet_x_800mf',
 'regnet_x_8gf',
 'regnet_y_128gf',
 'regnet_y_16gf',
 'regnet_y_1_6gf',
 'regnet_y_32gf',
 'regnet_y_3_2gf',
 'regnet_y_400mf',
 'regnet_y_800mf',
 'regnet_y_8gf',
 'resnet',
 'resnet101',
 'resnet152',
 'resnet18',
 'resnet34',
 'resnet50',
 'resnext101_32x8d',
 'resnext101_64x4d',
 'resnext50_32x4d',
 'segmentation',
 'shufflenet_v2_x0_5',
 'shufflenet_v2_x1_0',
 'shufflenet_v2_x1_5',
 'shufflenet_v2_x2_0',
 'shufflenetv2',
 'squeezenet',
 'squeezenet1_0',
 'squeezenet1_1',
 'swin_b',
 'swin_s',
 'swin_t',
 'swin_transformer',
 'swin_v2_b',
 'swin_v2_s',
 'swin_v2_t',
 'vgg',
 'vgg11',
 'vgg11_bn',
 'vgg13',
 'vgg13_bn',
 'vgg16',
 'vgg16_bn',
 'vgg19',
 'vgg19_bn',
 'video',
 'vision_transformer',
 'vit_b_16',
 'vit_b_32',
 'vit_h_14',
 'vit_l_16',
 'vit_l_32',
 'wide_resnet101_2',
 'wide_resnet50_2']

2. AlextNet¶

The AlexNet architecture won the 2012 ILSVRC by a large margin, with a top-5 test error rate (that is, the correct label must be in the top 5 predictions) of 15.4%. By comparison, the second-best submission, which wasn’t based on a deep network, trailed at 26.2%. This was a defining moment in the history of computer vision: the moment when the community started to realize the potential of deep learning for vision tasks. That leap was followed by constant improvement, with more modern architectures and training methods getting top-5 error rates as low as 3%.

By today’s standards, AlexNet is a rather small network! See the Alexnet structure below:

Alexnet_structure.jpg

In [4]:
# The AlexNet architecture
alexnet = models.AlexNet()
#alexnet

At this point, alexnet is an object that can run the AlexNet architecture. It’s not essential for us to understand the details of this architecture for now. For the time being, AlexNet is just an opaque object that can be called like a function. By providing alexnet with some precisely sized input data (we’ll see shortly what this input data should be), we will run a forward pass through the network. That is, the input will run through the first set of neurons, whose outputs will be fed to the next set of neurons, all the way to the final output. Practically speaking, assuming we have an input object of the right type, we can run the forward pass with output = alexnet(input). Should we do that?

3. ResNet¶

Let’s create an instance of a network now. We’ll pass an argument that will instruct the function to download the weights of resnet101 trained on the ImageNet dataset, with 1.2 million images and 1,000 categories.

While we’re staring at the download progress, we can take a minute to appreciate that resnet101 sports 44.5 million parameters—that’s a lot of parameters to optimize automatically!

In [5]:
# Instantiate a 101-layer convolutional neural network
resnet = models.resnet101(pretrained=True)
/usr/local/lib/python3.10/dist-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  warnings.warn(
/usr/local/lib/python3.10/dist-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet101_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet101_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)
Downloading: "https://download.pytorch.org/models/resnet101-63fe2227.pth" to /root/.cache/torch/hub/checkpoints/resnet101-63fe2227.pth
100%|██████████| 171M/171M [00:01<00:00, 130MB/s]
In [6]:
# The resnet101 architecture
resnet
Out[6]:
ResNet(
  (conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
  (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (relu): ReLU(inplace=True)
  (maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
  (layer1): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
      (downsample): Sequential(
        (0): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (2): Bottleneck(
      (conv1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
  )
  (layer2): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
      (downsample): Sequential(
        (0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (2): Bottleneck(
      (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (3): Bottleneck(
      (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
  )
  (layer3): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
      (downsample): Sequential(
        (0): Conv2d(512, 1024, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (2): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (3): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (4): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (5): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (6): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (7): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (8): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (9): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (10): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (11): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (12): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (13): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (14): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (15): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (16): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (17): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (18): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (19): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (20): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (21): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (22): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
  )
  (layer4): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
      (downsample): Sequential(
        (0): Conv2d(1024, 2048, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (2): Bottleneck(
      (conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
  (fc): Linear(in_features=2048, out_features=1000, bias=True)
)

4. Ready, set, almost run¶

The resnet variable can be called like a function, taking as input one or more images and producing an equal number of scores for each of the 1,000 ImageNet classes. Before we can do that, however, we have to preprocess the input images so they are the right size and so that their values (colors) sit roughly in the same numerical range. In order to do that, the torchvision module provides transforms, which allow us to quickly define pipelines of basic preprocessing functions:

In [7]:
# Preprocess the image
preprocess = transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize(
            mean=[0.485, 0.456, 0.406],
            std=[0.229, 0.224, 0.225]
        )])

The preprocess function will scale the input image to 256 × 256, crop the image to 224 × 224 around the center, transform it to a tensor (a PyTorch multidimensional array: in this case, a 3D array with color, height, and width), and normalize its RGB (red, green, blue) components so that they have defined means and standard deviations. These need to match what was presented to the network during training.

Now let's grab a picture, preprocess it, and then see what ResNet thinks of it.

In [8]:
# Import an image
img = Image.open("/content/drive/MyDrive/IS675_data/bobby.jpg")
In [9]:
img
Out[9]:
In [13]:
# Preprocess the image
img_t = preprocess(img)
img_t
Out[13]:
tensor([[[-0.6281, -0.6452, -0.6623,  ...,  0.0056, -0.0287, -0.0629],
         [-0.7137, -0.6965, -0.6965,  ...,  0.0227,  0.0227,  0.0056],
         [-0.7137, -0.7137, -0.7137,  ...,  0.0398,  0.0741,  0.0569],
         ...,
         [ 1.4440,  1.4269,  1.4783,  ...,  0.6049,  0.6221,  0.6906],
         [ 1.4269,  1.4440,  1.4783,  ...,  0.6906,  0.6734,  0.7077],
         [ 1.4612,  1.4783,  1.5297,  ...,  0.6906,  0.7248,  0.7591]],

        [[-1.2829, -1.2829, -1.2829,  ..., -0.6352, -0.6702, -0.7052],
         [-1.2654, -1.2654, -1.2654,  ..., -0.6176, -0.6527, -0.7052],
         [-1.2479, -1.2479, -1.2654,  ..., -0.6176, -0.6001, -0.6527],
         ...,
         [ 0.7829,  0.8004,  0.8704,  ..., -0.2850, -0.2675, -0.2150],
         [ 0.7654,  0.8354,  0.9055,  ..., -0.2150, -0.2150, -0.1625],
         [ 0.8004,  0.8529,  0.9230,  ..., -0.1800, -0.1275, -0.0749]],

        [[-1.4907, -1.4559, -1.4210,  ..., -1.0376, -1.0898, -1.1421],
         [-1.5081, -1.4559, -1.4210,  ..., -1.0376, -1.0898, -1.1421],
         [-1.4907, -1.4733, -1.4559,  ..., -1.0376, -1.0550, -1.0898],
         ...,
         [-0.5321, -0.5147, -0.4624,  ..., -1.3687, -1.2816, -1.1596],
         [-0.5321, -0.4798, -0.4275,  ..., -1.2816, -1.2293, -1.1073],
         [-0.4624, -0.4101, -0.3578,  ..., -1.2467, -1.1421, -1.0550]]])

Then we can reshape, crop, and normalize the input tensor in a way that the network expects. We’ll understand more of this in the next chapters; hold tight for now:

In [14]:
# Adding a dimension to our image to make it a batch of one image!
batch_t = torch.unsqueeze(img_t, 0)
batch_t
Out[14]:
tensor([[[[-0.6281, -0.6452, -0.6623,  ...,  0.0056, -0.0287, -0.0629],
          [-0.7137, -0.6965, -0.6965,  ...,  0.0227,  0.0227,  0.0056],
          [-0.7137, -0.7137, -0.7137,  ...,  0.0398,  0.0741,  0.0569],
          ...,
          [ 1.4440,  1.4269,  1.4783,  ...,  0.6049,  0.6221,  0.6906],
          [ 1.4269,  1.4440,  1.4783,  ...,  0.6906,  0.6734,  0.7077],
          [ 1.4612,  1.4783,  1.5297,  ...,  0.6906,  0.7248,  0.7591]],

         [[-1.2829, -1.2829, -1.2829,  ..., -0.6352, -0.6702, -0.7052],
          [-1.2654, -1.2654, -1.2654,  ..., -0.6176, -0.6527, -0.7052],
          [-1.2479, -1.2479, -1.2654,  ..., -0.6176, -0.6001, -0.6527],
          ...,
          [ 0.7829,  0.8004,  0.8704,  ..., -0.2850, -0.2675, -0.2150],
          [ 0.7654,  0.8354,  0.9055,  ..., -0.2150, -0.2150, -0.1625],
          [ 0.8004,  0.8529,  0.9230,  ..., -0.1800, -0.1275, -0.0749]],

         [[-1.4907, -1.4559, -1.4210,  ..., -1.0376, -1.0898, -1.1421],
          [-1.5081, -1.4559, -1.4210,  ..., -1.0376, -1.0898, -1.1421],
          [-1.4907, -1.4733, -1.4559,  ..., -1.0376, -1.0550, -1.0898],
          ...,
          [-0.5321, -0.5147, -0.4624,  ..., -1.3687, -1.2816, -1.1596],
          [-0.5321, -0.4798, -0.4275,  ..., -1.2816, -1.2293, -1.1073],
          [-0.4624, -0.4101, -0.3578,  ..., -1.2467, -1.1421, -1.0550]]]])

We’re now ready to run our model.

5. Run¶

The process of running a trained model on new data is called inference in deep learning circles. In order to do inference, we need to put the network in eval mode: (If we forget to do that, some pretrained models, like batch normalization and dropout, will not produce meaningful answers, just because of the way they work internally.)

In [15]:
# Put the model in eval mode
resnet.eval()
Out[15]:
ResNet(
  (conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
  (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (relu): ReLU(inplace=True)
  (maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
  (layer1): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
      (downsample): Sequential(
        (0): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (2): Bottleneck(
      (conv1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
  )
  (layer2): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
      (downsample): Sequential(
        (0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (2): Bottleneck(
      (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (3): Bottleneck(
      (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
  )
  (layer3): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
      (downsample): Sequential(
        (0): Conv2d(512, 1024, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (2): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (3): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (4): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (5): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (6): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (7): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (8): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (9): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (10): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (11): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (12): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (13): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (14): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (15): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (16): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (17): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (18): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (19): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (20): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (21): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (22): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
  )
  (layer4): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
      (downsample): Sequential(
        (0): Conv2d(1024, 2048, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
    (2): Bottleneck(
      (conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
    )
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
  (fc): Linear(in_features=2048, out_features=1000, bias=True)
)
In [16]:
# Inference
out = resnet(batch_t)
out
Out[16]:
tensor([[-3.4997e+00, -1.6490e+00, -2.4391e+00, -3.2243e+00, -3.2465e+00,
         -1.3218e+00, -2.0395e+00, -2.5405e+00, -1.3043e+00, -2.8827e+00,
         -1.6696e+00, -1.2838e+00, -2.6184e+00, -2.9750e+00, -2.4380e+00,
         -2.8256e+00, -3.3083e+00, -7.9667e-01, -6.7075e-01, -1.2162e+00,
         -3.0311e+00, -3.9593e+00, -2.2631e+00, -1.0843e+00, -9.7915e-01,
         -1.0742e+00, -3.0908e+00, -2.4751e+00, -2.2153e+00, -3.1932e+00,
         -3.2964e+00, -1.8507e+00, -2.0642e+00, -2.1202e+00, -1.8665e+00,
         -3.2375e+00, -1.1210e+00, -1.1321e+00, -1.1657e+00, -9.0361e-01,
         -4.5209e-01, -1.4986e+00,  1.4366e+00,  1.2994e-01, -1.8379e+00,
         -1.4815e+00,  9.7278e-01, -9.3662e-01, -3.0276e+00, -2.7341e+00,
         -2.5960e+00, -2.0591e+00, -1.8170e+00, -1.9437e+00, -1.7875e+00,
         -1.3029e+00, -4.5200e-01, -2.0560e+00, -3.2882e+00, -4.7583e-01,
         -3.6261e-01, -1.1650e+00, -7.3942e-01, -1.4489e+00, -1.5039e+00,
         -2.1096e+00, -1.7095e+00, -4.1315e-01, -1.9146e+00, -1.5095e+00,
         -1.2012e+00, -1.3111e+00, -1.0662e+00,  1.2968e-01, -3.8648e-01,
         -2.4670e-01, -8.7028e-01,  5.9567e-01, -8.8329e-01,  1.4535e+00,
         -2.6384e+00, -3.6589e+00, -2.3378e-01, -4.9410e-02, -2.2602e+00,
         -2.3605e+00, -1.4038e+00,  2.4098e-01, -1.0531e+00, -2.9106e+00,
         -2.5321e+00, -2.2253e+00,  4.4062e-01, -1.3269e+00, -2.0447e-02,
         -2.8696e+00, -5.5842e-01, -1.3959e+00, -2.9274e+00, -1.9038e+00,
         -4.2433e+00, -2.9701e+00, -2.0552e+00, -2.4290e+00, -2.7691e+00,
         -4.0207e+00, -3.6511e+00, -4.8903e-01, -9.8899e-01, -1.8995e+00,
         -3.5459e+00, -1.4606e+00,  1.1054e+00, -6.9230e-01, -7.4877e-01,
         -2.1745e+00, -2.2471e+00, -5.3973e-01, -1.5121e+00, -2.5998e+00,
         -3.9889e+00, -9.7558e-01, -5.5630e-01, -8.6530e-01, -1.2110e+00,
         -7.3269e-01, -1.0847e+00, -2.0944e+00, -4.0080e+00, -3.3433e-01,
         -2.6919e+00, -2.9908e+00, -1.8793e+00, -1.8126e+00, -1.2953e+00,
         -2.2093e+00, -2.0246e+00, -3.1878e+00, -3.2739e+00, -2.7969e+00,
         -4.1270e-01, -3.7697e+00, -2.4744e+00, -2.6133e+00, -2.7514e+00,
         -2.6512e+00, -3.2751e+00, -4.3150e+00, -4.2214e+00, -3.5920e+00,
         -1.2186e+00,  2.3577e+00,  1.9648e+00,  2.2958e+00,  5.0050e+00,
          1.0229e+00,  2.9914e+00,  8.8863e-01,  2.2502e+00,  6.1697e+00,
          4.2833e+00,  4.5931e+00,  7.4686e+00,  4.3448e+00,  4.8805e+00,
          5.8076e+00,  3.9792e+00,  3.5555e+00,  9.5253e+00,  1.1172e+00,
          3.3158e+00,  1.9011e+00, -5.1524e-01,  1.3985e+00,  1.8150e+00,
          5.6135e+00,  5.7316e+00,  2.1489e+00,  2.4986e+00,  5.5508e-01,
          1.8658e+00,  1.4871e+00,  3.4364e+00,  4.4125e-01,  4.2860e+00,
          4.3832e+00,  1.3132e+00,  1.6830e+00,  1.1566e+00,  2.1403e+00,
          9.1246e-01,  3.0220e+00,  3.5398e+00,  2.8193e+00,  1.9450e+00,
          8.4405e-02,  2.0383e+00,  2.7105e+00,  1.0817e+00,  1.9388e+00,
          3.5678e+00,  2.3330e+00,  2.5193e+00,  1.3185e+00,  3.6052e+00,
          7.7896e+00,  4.4019e+00,  1.5813e+01,  1.2201e+01,  5.1687e+00,
          1.9371e+00,  5.5298e+00,  6.2411e+00,  7.5443e+00,  5.9197e+00,
          7.0106e+00,  5.7829e+00,  2.6940e+00,  5.3224e+00,  9.9329e+00,
          6.4925e+00,  2.4706e+00,  5.6329e+00,  3.4480e+00,  2.0878e+00,
          1.2667e+00,  2.5974e+00,  5.6932e+00,  7.1867e-01,  1.0250e+00,
          4.8133e+00,  6.0352e+00,  3.9970e+00, -1.7816e+00,  3.6096e+00,
          2.8417e+00,  2.8397e+00,  1.2274e+00,  4.2220e+00,  4.0891e+00,
          3.3476e+00,  2.3510e+00,  2.6175e-01,  8.3823e-01,  5.1252e+00,
         -7.6088e-01,  1.9809e+00,  2.8381e+00,  2.4766e+00,  1.8687e+00,
          5.8744e-01,  3.4939e+00, -5.0017e-01,  1.8486e+00,  4.4583e-01,
          2.4148e+00,  3.4823e+00,  5.1915e+00,  1.8092e+00,  3.9559e+00,
          4.3692e+00,  3.1688e-01, -4.8611e-01,  5.6470e+00,  3.6534e+00,
          2.4436e+00,  3.6937e+00,  3.3958e+00, -9.6209e-01, -2.2416e-01,
          1.2344e-01,  1.1682e+00, -5.7350e-02,  4.4999e+00, -1.8748e+00,
         -2.6818e+00, -1.6847e+00, -6.7984e-01, -1.5588e+00, -2.1798e+00,
         -1.0431e+00,  1.5370e+00,  7.6586e-01, -1.7835e+00,  6.3795e-01,
          8.8728e-01, -2.1297e+00, -2.1428e+00, -1.5652e+00, -4.8034e+00,
         -1.0672e+00, -9.5017e-01, -2.0649e+00,  2.0019e+00, -6.4177e-01,
         -2.5378e+00, -1.5008e+00, -3.1975e+00, -1.9334e+00, -5.8521e-01,
         -1.0363e+00,  1.3329e+00, -1.4872e+00, -2.0102e+00,  1.3183e+00,
          4.2174e-01, -3.5856e-01,  7.4732e-01, -2.7741e-01, -7.5281e-01,
         -5.5096e-01,  1.0833e+00,  1.3690e-01,  5.3099e-02,  1.2991e+00,
         -2.0118e+00, -1.7402e+00,  1.1788e+00, -4.2505e-01,  3.0964e-01,
         -1.4589e+00, -2.6225e+00, -2.4953e+00, -2.1100e-01, -2.3407e+00,
         -1.5377e+00, -2.4802e+00, -8.0411e-01,  2.1877e-01, -2.8007e+00,
         -1.9041e+00, -9.3844e-01, -3.4383e-01, -4.4378e-01, -7.5068e-01,
         -2.5060e+00, -1.9128e+00, -2.1313e+00, -5.4917e-01,  1.7571e-01,
         -2.0437e+00, -1.7883e+00, -2.4830e+00, -3.8768e+00, -4.4253e+00,
         -2.1819e+00, -2.0485e+00, -3.7339e+00, -1.9185e+00, -3.4523e+00,
         -2.0103e+00, -2.2686e+00, -7.9842e-01, -5.0350e-01, -2.9496e+00,
         -1.6959e+00, -2.9049e-01, -2.8050e+00, -1.8296e+00,  9.0892e-02,
         -2.1747e+00, -1.5452e+00, -1.2558e+00, -9.5389e-01, -2.5265e+00,
         -1.3665e+00, -3.8682e+00, -4.3242e+00, -1.5148e+00, -1.9113e+00,
         -2.9872e+00, -2.9385e+00, -4.8581e+00, -2.4930e+00, -1.3708e+00,
         -2.8970e+00, -1.2814e+00, -5.6305e-01, -2.8289e+00, -1.8666e+00,
         -2.3429e+00, -3.4331e+00, -2.5701e+00, -3.8991e+00, -4.4677e+00,
         -3.5612e+00, -9.3197e-01, -2.3963e+00, -2.0673e+00, -1.8581e+00,
         -3.2819e+00, -3.5219e+00, -2.8122e+00, -1.9661e+00, -3.7761e+00,
         -1.1609e+00, -2.7743e+00,  2.5781e-01, -1.7330e-01, -3.0291e-01,
          1.6415e-01,  1.7316e+00,  1.8974e+00, -3.1201e+00, -3.7529e+00,
         -2.6532e+00, -6.6325e-01, -8.5835e-01, -1.3166e+00,  4.9872e-01,
         -2.3256e+00,  4.7837e+00,  1.4947e+00, -1.6190e+00,  3.3725e+00,
          3.0711e+00,  1.5550e+00,  7.3323e-01,  1.2925e+00,  1.6615e+00,
          9.5885e-02,  1.3348e+00,  2.9193e-02,  9.8533e-01, -5.1703e-01,
         -1.3542e+00,  5.3265e-01,  2.7115e-01,  2.8676e+00,  2.2308e+00,
          1.8468e+00,  1.3861e+00,  1.1031e+00,  2.3657e+00,  6.4129e+00,
          1.8896e+00,  5.8361e-01, -1.5098e+00, -1.8949e+00, -2.6363e+00,
          1.5000e+00, -5.3598e-01, -6.9934e-02,  2.0223e+00, -1.7648e+00,
          4.1311e-01,  1.6696e+00,  7.3901e-01,  2.1839e+00, -1.4280e+00,
         -2.4708e+00,  4.8153e-01,  3.3817e+00, -2.9651e-01,  6.8334e-02,
          1.1068e+00, -1.3973e+00,  1.5518e+00, -2.2464e+00, -3.6723e-01,
         -1.5056e+00, -2.1660e+00,  2.7747e+00,  2.5229e+00,  1.8796e-01,
          4.6500e-01, -1.9905e+00, -1.6224e+00, -7.7251e-01, -1.1583e+00,
         -3.0849e-01, -1.4386e+00,  2.2714e+00, -4.5280e-01, -8.7969e-01,
          4.1363e-01, -1.4095e+00, -2.8906e-01,  3.4809e+00,  4.3324e-01,
          4.2387e-01, -1.1204e+00,  1.9781e-01, -2.6528e+00, -2.8554e+00,
         -6.6075e-01, -3.1026e-01,  7.8641e-01,  1.8965e-01, -4.5897e-01,
         -5.5900e-01, -2.8286e-01,  1.6559e+00,  7.1156e-02, -4.2351e-01,
          1.1153e+00,  2.2649e+00, -1.0259e+00,  9.7924e-01,  1.0194e+00,
         -2.6048e+00, -5.5008e-01,  2.1353e+00, -9.3641e-01,  4.3960e-02,
         -3.9618e-01,  1.5395e+00, -1.0598e+00,  1.8685e+00,  1.4945e+00,
         -4.4445e+00, -1.7148e-01, -1.6854e-01, -7.5243e-01,  1.8241e+00,
          3.3237e+00,  7.4655e-01, -7.6160e-01,  1.6335e+00,  1.9438e+00,
          1.2983e+00, -1.5951e+00,  2.2410e+00,  9.3765e-01, -6.2485e-01,
         -1.3969e+00,  1.6010e+00,  2.9321e-02,  8.8535e-01,  9.6981e-01,
          5.0879e-01,  3.9120e-01,  1.0257e+00,  6.5950e-01,  3.3687e+00,
         -1.0271e+00, -2.9353e+00, -7.5909e-01, -7.9742e-01,  7.0959e+00,
         -3.3062e+00,  1.3640e+00,  1.7516e+00,  3.7723e+00, -1.1675e+00,
          5.5042e-01, -1.8410e+00, -1.3717e+00,  4.1994e-01,  2.6614e+00,
          1.4409e+00,  8.2754e-01,  4.9228e+00,  1.5345e+00, -3.6179e+00,
         -2.2328e+00, -1.3824e+00, -8.8731e-01,  2.2056e+00,  2.0819e+00,
         -9.1448e-01, -1.7883e+00, -1.4364e+00,  1.0453e+00, -7.3781e-01,
         -1.4398e+00,  4.6884e-01,  8.8505e-01,  6.7727e-01, -1.1915e+00,
         -1.4081e+00, -8.5758e-01,  1.1674e+00, -1.8481e+00,  2.5000e+00,
          3.4197e-01, -1.7550e+00, -8.9500e-01,  2.9997e+00,  2.2094e+00,
         -8.3813e-01, -2.0126e+00,  9.3872e-01, -2.1452e+00,  3.8366e-02,
          2.4887e-01, -1.0520e-01, -6.5314e-01,  8.0483e-01,  4.0207e+00,
         -1.2178e+00,  2.6536e+00, -1.8085e+00,  7.5238e-01,  5.0293e-01,
          1.1508e-02, -7.7166e-01,  4.5107e-01,  1.3278e+00,  8.2390e-01,
         -6.1212e-01,  3.0906e-02,  2.6237e-01, -2.1468e+00, -1.4071e+00,
         -9.9407e-02, -4.3217e-01,  4.8178e-01,  1.2984e+00,  7.5574e-01,
          8.5152e-02, -1.3871e-01, -8.5174e-01,  8.1902e-01,  7.0772e-01,
          2.1356e+00,  7.1527e-01, -1.9824e-01,  2.4378e+00,  1.8625e+00,
          1.0683e+00,  1.0118e+00,  1.4432e+00, -1.9515e-02, -1.6767e-01,
         -1.6930e+00, -1.2930e+00, -1.3439e+00, -3.4781e+00,  5.9320e-01,
          1.0796e+00, -1.8919e-02, -3.9901e-01, -4.6973e-01, -3.7133e-01,
         -1.5430e+00,  3.1287e-01, -1.8760e-01,  3.2949e+00,  1.8577e+00,
         -1.5585e+00,  2.9257e+00, -7.3537e-01,  6.2361e-02, -4.3241e-02,
         -1.0193e+00,  1.1614e+00,  6.1005e-01,  1.4297e-01, -3.4956e+00,
          1.3337e+00,  3.8960e-01, -1.2900e+00, -1.2823e+00, -1.6482e+00,
          1.9285e+00,  2.7966e-02, -9.3298e-01,  6.6934e-01,  2.9179e+00,
          1.7228e+00,  2.1640e-01, -8.5777e-01, -2.0391e+00,  1.6510e+00,
         -2.3326e-01,  7.4309e-01,  1.5400e+00, -1.4460e+00, -1.2114e+00,
          5.2273e-01, -6.4955e-01,  4.8661e-02,  2.6569e+00, -1.6518e+00,
         -3.3896e+00,  2.1350e+00,  9.0318e-01,  1.3904e+00, -3.6350e-01,
          2.0223e-01,  1.4554e+00, -1.1211e+00,  1.8399e+00, -1.2923e+00,
         -2.1249e+00, -8.6377e-01, -5.1445e-01, -1.2872e+00,  1.1431e+00,
         -1.4240e+00, -7.6690e-01, -3.5563e-01,  2.1923e+00, -1.9831e-01,
         -1.8438e+00,  9.6277e-02,  1.7458e+00,  1.7894e-01,  8.0167e-01,
          4.7614e+00, -7.3425e-01, -1.6478e+00, -8.1854e-01, -1.5260e+00,
          3.4332e+00,  4.9594e-01, -3.4153e-01, -5.1844e-01,  3.1711e-01,
          1.8911e+00, -1.2033e+00, -2.0005e+00, -1.2113e-01, -7.0711e-01,
         -2.5450e+00, -6.7447e-02,  8.0279e-01, -1.8256e+00,  2.9197e-01,
         -1.1033e+00,  1.5367e+00,  2.9367e+00,  5.3938e-01, -7.6932e-01,
         -9.2092e-01, -1.7367e+00, -1.0012e+00,  9.3510e-01, -4.1549e-02,
         -2.3282e+00,  2.2907e+00,  9.3231e-01,  2.0218e+00, -1.7313e+00,
          2.6369e+00,  3.8262e+00,  6.9461e-01, -2.5070e-03, -9.9190e-01,
          2.1992e+00,  1.4213e+00,  4.3468e-01, -1.3524e+00, -8.5725e-01,
          5.7249e-01, -6.5436e-01,  5.3128e-01,  1.1928e+00,  9.9297e-01,
          2.8021e+00, -3.0989e+00,  3.5913e+00, -1.7487e-01, -7.0676e-02,
         -2.0895e+00, -1.4625e+00,  3.0381e+00, -4.9794e-01,  2.0560e-01,
         -1.4915e-01,  2.2701e+00,  3.2218e-01,  9.6720e-01, -1.1802e+00,
          3.3260e+00, -1.8322e+00,  3.2782e+00, -1.7115e-01,  1.3883e+00,
          6.0959e-01,  4.1543e-01, -7.9037e-01,  3.6867e-01,  1.9525e+00,
          4.2260e-01,  2.4431e+00, -1.9236e+00,  1.2817e-02,  2.2662e+00,
         -1.6735e+00, -7.1518e-01,  1.3959e+00, -2.5440e-02,  7.7251e-01,
          3.0006e+00, -9.7998e-01, -1.3561e+00,  2.3763e-01,  2.3640e+00,
          1.9144e+00,  1.8060e+00,  8.6761e-01,  4.2975e+00,  5.3407e-01,
         -1.4330e+00, -5.0711e-01,  3.0123e+00, -8.6910e-01,  3.2884e+00,
         -6.6958e-01, -2.6305e+00, -3.1957e+00, -2.9512e+00,  1.2077e+00,
          5.6051e+00,  2.7644e-01, -3.1766e+00,  2.5924e+00, -3.3277e-01,
          1.2678e-01,  2.3057e+00, -1.4350e+00,  3.3605e+00, -3.3686e+00,
         -6.4941e-01,  1.1768e+00, -2.6352e+00,  1.6955e+00, -1.4452e+00,
         -2.6586e+00, -3.6629e+00, -1.7268e+00,  1.1226e+00,  1.9893e+00,
         -1.6089e-01,  1.2605e+00,  1.2145e+00,  2.6831e+00, -1.8129e+00,
         -1.8627e+00,  1.5719e+00, -2.0970e+00, -2.3203e+00,  9.8824e-01,
         -3.8493e+00, -1.1166e+00,  8.9900e-01, -3.4761e-01, -3.5048e+00,
          2.0611e+00,  5.2646e-01,  6.1806e-01, -1.1572e-01,  8.0079e-01,
          8.0897e-01,  1.0604e+00, -1.8259e+00,  2.6638e-01, -1.1470e+00,
          8.3008e-01,  1.0902e+00,  9.0343e+00, -9.1355e-01, -9.0900e-01,
          1.1300e+00, -3.8219e-01, -1.7321e+00,  1.0250e-01,  2.0110e-01,
         -1.4002e+00,  8.6910e-01, -4.4545e-01, -7.9011e-01, -3.1371e+00,
         -3.6089e-01,  1.9827e-01, -2.1941e+00,  2.8923e+00,  4.7277e-01,
          1.4522e+00, -3.4306e+00,  1.4684e+00, -2.5527e+00, -2.9418e+00,
         -7.8795e-01,  2.8671e+00, -1.2269e+00, -6.5231e-01,  3.6328e+00,
          1.1509e+00,  1.1695e+00,  3.3913e+00,  9.2258e-01, -1.1167e+00,
          1.0877e+00,  1.9697e-01, -7.4067e-01, -2.9546e+00, -1.7774e-01,
          1.5686e+00, -4.4219e-01,  1.6109e+00,  1.0286e+00, -1.2968e+00,
         -1.7997e+00, -4.9459e-01,  5.5377e-01,  2.3711e+00,  5.1920e-01,
         -1.3707e+00, -2.3571e+00, -7.6039e-01,  1.3218e+00,  2.9741e+00,
         -2.6011e-01, -6.0607e-01, -4.2340e-02, -7.4892e-01, -7.3591e-02,
          2.7876e+00,  3.2539e-01, -1.1395e+00, -1.4875e+00, -4.3684e+00,
         -3.6234e-01, -3.4597e-01, -2.0751e+00,  3.6143e-01, -1.4748e+00,
         -6.3914e-01, -2.0747e+00, -9.1273e-01, -4.0377e-01, -3.3568e-01,
         -1.8499e+00, -1.7759e+00, -6.6116e-01,  6.3051e-01,  3.7449e+00,
          2.1652e+00,  4.1343e+00,  1.6970e+00,  8.3944e-01,  1.2930e+00,
          1.0517e+00,  1.4064e+00,  1.3186e+00, -7.6195e-01,  2.2661e+00,
          1.4491e-01, -7.7404e-01,  1.0131e+00,  8.7297e-01, -6.4795e-01,
         -3.4213e-01, -1.5297e+00,  2.0686e+00,  2.5062e+00,  2.1082e-01,
          7.5025e-01,  1.8742e-02, -1.7822e+00,  1.9568e+00,  4.3805e-01,
          1.2335e+00,  7.7087e-01, -1.0329e+00,  1.5594e-01, -3.6626e-01,
          6.3100e-02,  3.2490e+00,  6.4807e-01, -1.5173e-01,  9.3229e-01,
         -6.0559e-02, -1.1985e+00,  1.4659e-01,  3.9304e-01,  8.8496e-01,
         -1.9458e+00,  1.2217e-01, -1.5016e+00, -1.8235e+00, -3.8595e+00,
          1.8626e-01, -2.9675e+00,  5.4217e-01, -7.8127e-01, -2.6196e+00,
         -4.4958e+00,  5.7119e-01, -1.3398e+00, -3.8117e+00, -7.8630e-01,
         -5.6788e-01, -2.5453e+00,  2.4054e+00,  6.5602e-01,  4.7648e-01,
          2.8750e+00, -3.7451e+00,  1.5124e+00, -3.2777e+00, -2.4971e+00,
         -3.2263e-01,  1.2816e-01, -1.1752e+00,  3.4434e+00,  4.4534e+00]],
       grad_fn=<AddmmBackward0>)

A staggering set of operations involving 44.5 million parameters has just happened, producing a vector of 1,000 scores, one per ImageNet class. That didn’t take long, did it?

We now need to find out the label of the class that received the highest score.

In [17]:
# The list of predicted labels
with open('/content/drive/MyDrive/IS675_data/imagenet_classes.txt') as f:
    labels = [line.strip() for line in f.readlines()]
labels
Out[17]:
['tench, Tinca tinca',
 'goldfish, Carassius auratus',
 'great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias',
 'tiger shark, Galeocerdo cuvieri',
 'hammerhead, hammerhead shark',
 'electric ray, crampfish, numbfish, torpedo',
 'stingray',
 'cock',
 'hen',
 'ostrich, Struthio camelus',
 'brambling, Fringilla montifringilla',
 'goldfinch, Carduelis carduelis',
 'house finch, linnet, Carpodacus mexicanus',
 'junco, snowbird',
 'indigo bunting, indigo finch, indigo bird, Passerina cyanea',
 'robin, American robin, Turdus migratorius',
 'bulbul',
 'jay',
 'magpie',
 'chickadee',
 'water ouzel, dipper',
 'kite',
 'bald eagle, American eagle, Haliaeetus leucocephalus',
 'vulture',
 'great grey owl, great gray owl, Strix nebulosa',
 'European fire salamander, Salamandra salamandra',
 'common newt, Triturus vulgaris',
 'eft',
 'spotted salamander, Ambystoma maculatum',
 'axolotl, mud puppy, Ambystoma mexicanum',
 'bullfrog, Rana catesbeiana',
 'tree frog, tree-frog',
 'tailed frog, bell toad, ribbed toad, tailed toad, Ascaphus trui',
 'loggerhead, loggerhead turtle, Caretta caretta',
 'leatherback turtle, leatherback, leathery turtle, Dermochelys coriacea',
 'mud turtle',
 'terrapin',
 'box turtle, box tortoise',
 'banded gecko',
 'common iguana, iguana, Iguana iguana',
 'American chameleon, anole, Anolis carolinensis',
 'whiptail, whiptail lizard',
 'agama',
 'frilled lizard, Chlamydosaurus kingi',
 'alligator lizard',
 'Gila monster, Heloderma suspectum',
 'green lizard, Lacerta viridis',
 'African chameleon, Chamaeleo chamaeleon',
 'Komodo dragon, Komodo lizard, dragon lizard, giant lizard, Varanus komodoensis',
 'African crocodile, Nile crocodile, Crocodylus niloticus',
 'American alligator, Alligator mississipiensis',
 'triceratops',
 'thunder snake, worm snake, Carphophis amoenus',
 'ringneck snake, ring-necked snake, ring snake',
 'hognose snake, puff adder, sand viper',
 'green snake, grass snake',
 'king snake, kingsnake',
 'garter snake, grass snake',
 'water snake',
 'vine snake',
 'night snake, Hypsiglena torquata',
 'boa constrictor, Constrictor constrictor',
 'rock python, rock snake, Python sebae',
 'Indian cobra, Naja naja',
 'green mamba',
 'sea snake',
 'horned viper, cerastes, sand viper, horned asp, Cerastes cornutus',
 'diamondback, diamondback rattlesnake, Crotalus adamanteus',
 'sidewinder, horned rattlesnake, Crotalus cerastes',
 'trilobite',
 'harvestman, daddy longlegs, Phalangium opilio',
 'scorpion',
 'black and gold garden spider, Argiope aurantia',
 'barn spider, Araneus cavaticus',
 'garden spider, Aranea diademata',
 'black widow, Latrodectus mactans',
 'tarantula',
 'wolf spider, hunting spider',
 'tick',
 'centipede',
 'black grouse',
 'ptarmigan',
 'ruffed grouse, partridge, Bonasa umbellus',
 'prairie chicken, prairie grouse, prairie fowl',
 'peacock',
 'quail',
 'partridge',
 'African grey, African gray, Psittacus erithacus',
 'macaw',
 'sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita',
 'lorikeet',
 'coucal',
 'bee eater',
 'hornbill',
 'hummingbird',
 'jacamar',
 'toucan',
 'drake',
 'red-breasted merganser, Mergus serrator',
 'goose',
 'black swan, Cygnus atratus',
 'tusker',
 'echidna, spiny anteater, anteater',
 'platypus, duckbill, duckbilled platypus, duck-billed platypus, Ornithorhynchus anatinus',
 'wallaby, brush kangaroo',
 'koala, koala bear, kangaroo bear, native bear, Phascolarctos cinereus',
 'wombat',
 'jellyfish',
 'sea anemone, anemone',
 'brain coral',
 'flatworm, platyhelminth',
 'nematode, nematode worm, roundworm',
 'conch',
 'snail',
 'slug',
 'sea slug, nudibranch',
 'chiton, coat-of-mail shell, sea cradle, polyplacophore',
 'chambered nautilus, pearly nautilus, nautilus',
 'Dungeness crab, Cancer magister',
 'rock crab, Cancer irroratus',
 'fiddler crab',
 'king crab, Alaska crab, Alaskan king crab, Alaska king crab, Paralithodes camtschatica',
 'American lobster, Northern lobster, Maine lobster, Homarus americanus',
 'spiny lobster, langouste, rock lobster, crawfish, crayfish, sea crawfish',
 'crayfish, crawfish, crawdad, crawdaddy',
 'hermit crab',
 'isopod',
 'white stork, Ciconia ciconia',
 'black stork, Ciconia nigra',
 'spoonbill',
 'flamingo',
 'little blue heron, Egretta caerulea',
 'American egret, great white heron, Egretta albus',
 'bittern',
 'crane',
 'limpkin, Aramus pictus',
 'European gallinule, Porphyrio porphyrio',
 'American coot, marsh hen, mud hen, water hen, Fulica americana',
 'bustard',
 'ruddy turnstone, Arenaria interpres',
 'red-backed sandpiper, dunlin, Erolia alpina',
 'redshank, Tringa totanus',
 'dowitcher',
 'oystercatcher, oyster catcher',
 'pelican',
 'king penguin, Aptenodytes patagonica',
 'albatross, mollymawk',
 'grey whale, gray whale, devilfish, Eschrichtius gibbosus, Eschrichtius robustus',
 'killer whale, killer, orca, grampus, sea wolf, Orcinus orca',
 'dugong, Dugong dugon',
 'sea lion',
 'Chihuahua',
 'Japanese spaniel',
 'Maltese dog, Maltese terrier, Maltese',
 'Pekinese, Pekingese, Peke',
 'Shih-Tzu',
 'Blenheim spaniel',
 'papillon',
 'toy terrier',
 'Rhodesian ridgeback',
 'Afghan hound, Afghan',
 'basset, basset hound',
 'beagle',
 'bloodhound, sleuthhound',
 'bluetick',
 'black-and-tan coonhound',
 'Walker hound, Walker foxhound',
 'English foxhound',
 'redbone',
 'borzoi, Russian wolfhound',
 'Irish wolfhound',
 'Italian greyhound',
 'whippet',
 'Ibizan hound, Ibizan Podenco',
 'Norwegian elkhound, elkhound',
 'otterhound, otter hound',
 'Saluki, gazelle hound',
 'Scottish deerhound, deerhound',
 'Weimaraner',
 'Staffordshire bullterrier, Staffordshire bull terrier',
 'American Staffordshire terrier, Staffordshire terrier, American pit bull terrier, pit bull terrier',
 'Bedlington terrier',
 'Border terrier',
 'Kerry blue terrier',
 'Irish terrier',
 'Norfolk terrier',
 'Norwich terrier',
 'Yorkshire terrier',
 'wire-haired fox terrier',
 'Lakeland terrier',
 'Sealyham terrier, Sealyham',
 'Airedale, Airedale terrier',
 'cairn, cairn terrier',
 'Australian terrier',
 'Dandie Dinmont, Dandie Dinmont terrier',
 'Boston bull, Boston terrier',
 'miniature schnauzer',
 'giant schnauzer',
 'standard schnauzer',
 'Scotch terrier, Scottish terrier, Scottie',
 'Tibetan terrier, chrysanthemum dog',
 'silky terrier, Sydney silky',
 'soft-coated wheaten terrier',
 'West Highland white terrier',
 'Lhasa, Lhasa apso',
 'flat-coated retriever',
 'curly-coated retriever',
 'golden retriever',
 'Labrador retriever',
 'Chesapeake Bay retriever',
 'German short-haired pointer',
 'vizsla, Hungarian pointer',
 'English setter',
 'Irish setter, red setter',
 'Gordon setter',
 'Brittany spaniel',
 'clumber, clumber spaniel',
 'English springer, English springer spaniel',
 'Welsh springer spaniel',
 'cocker spaniel, English cocker spaniel, cocker',
 'Sussex spaniel',
 'Irish water spaniel',
 'kuvasz',
 'schipperke',
 'groenendael',
 'malinois',
 'briard',
 'kelpie',
 'komondor',
 'Old English sheepdog, bobtail',
 'Shetland sheepdog, Shetland sheep dog, Shetland',
 'collie',
 'Border collie',
 'Bouvier des Flandres, Bouviers des Flandres',
 'Rottweiler',
 'German shepherd, German shepherd dog, German police dog, alsatian',
 'Doberman, Doberman pinscher',
 'miniature pinscher',
 'Greater Swiss Mountain dog',
 'Bernese mountain dog',
 'Appenzeller',
 'EntleBucher',
 'boxer',
 'bull mastiff',
 'Tibetan mastiff',
 'French bulldog',
 'Great Dane',
 'Saint Bernard, St Bernard',
 'Eskimo dog, husky',
 'malamute, malemute, Alaskan malamute',
 'Siberian husky',
 'dalmatian, coach dog, carriage dog',
 'affenpinscher, monkey pinscher, monkey dog',
 'basenji',
 'pug, pug-dog',
 'Leonberg',
 'Newfoundland, Newfoundland dog',
 'Great Pyrenees',
 'Samoyed, Samoyede',
 'Pomeranian',
 'chow, chow chow',
 'keeshond',
 'Brabancon griffon',
 'Pembroke, Pembroke Welsh corgi',
 'Cardigan, Cardigan Welsh corgi',
 'toy poodle',
 'miniature poodle',
 'standard poodle',
 'Mexican hairless',
 'timber wolf, grey wolf, gray wolf, Canis lupus',
 'white wolf, Arctic wolf, Canis lupus tundrarum',
 'red wolf, maned wolf, Canis rufus, Canis niger',
 'coyote, prairie wolf, brush wolf, Canis latrans',
 'dingo, warrigal, warragal, Canis dingo',
 'dhole, Cuon alpinus',
 'African hunting dog, hyena dog, Cape hunting dog, Lycaon pictus',
 'hyena, hyaena',
 'red fox, Vulpes vulpes',
 'kit fox, Vulpes macrotis',
 'Arctic fox, white fox, Alopex lagopus',
 'grey fox, gray fox, Urocyon cinereoargenteus',
 'tabby, tabby cat',
 'tiger cat',
 'Persian cat',
 'Siamese cat, Siamese',
 'Egyptian cat',
 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor',
 'lynx, catamount',
 'leopard, Panthera pardus',
 'snow leopard, ounce, Panthera uncia',
 'jaguar, panther, Panthera onca, Felis onca',
 'lion, king of beasts, Panthera leo',
 'tiger, Panthera tigris',
 'cheetah, chetah, Acinonyx jubatus',
 'brown bear, bruin, Ursus arctos',
 'American black bear, black bear, Ursus americanus, Euarctos americanus',
 'ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus',
 'sloth bear, Melursus ursinus, Ursus ursinus',
 'mongoose',
 'meerkat, mierkat',
 'tiger beetle',
 'ladybug, ladybeetle, lady beetle, ladybird, ladybird beetle',
 'ground beetle, carabid beetle',
 'long-horned beetle, longicorn, longicorn beetle',
 'leaf beetle, chrysomelid',
 'dung beetle',
 'rhinoceros beetle',
 'weevil',
 'fly',
 'bee',
 'ant, emmet, pismire',
 'grasshopper, hopper',
 'cricket',
 'walking stick, walkingstick, stick insect',
 'cockroach, roach',
 'mantis, mantid',
 'cicada, cicala',
 'leafhopper',
 'lacewing, lacewing fly',
 "dragonfly, darning needle, devil's darning needle, sewing needle, snake feeder, snake doctor, mosquito hawk, skeeter hawk",
 'damselfly',
 'admiral',
 'ringlet, ringlet butterfly',
 'monarch, monarch butterfly, milkweed butterfly, Danaus plexippus',
 'cabbage butterfly',
 'sulphur butterfly, sulfur butterfly',
 'lycaenid, lycaenid butterfly',
 'starfish, sea star',
 'sea urchin',
 'sea cucumber, holothurian',
 'wood rabbit, cottontail, cottontail rabbit',
 'hare',
 'Angora, Angora rabbit',
 'hamster',
 'porcupine, hedgehog',
 'fox squirrel, eastern fox squirrel, Sciurus niger',
 'marmot',
 'beaver',
 'guinea pig, Cavia cobaya',
 'sorrel',
 'zebra',
 'hog, pig, grunter, squealer, Sus scrofa',
 'wild boar, boar, Sus scrofa',
 'warthog',
 'hippopotamus, hippo, river horse, Hippopotamus amphibius',
 'ox',
 'water buffalo, water ox, Asiatic buffalo, Bubalus bubalis',
 'bison',
 'ram, tup',
 'bighorn, bighorn sheep, cimarron, Rocky Mountain bighorn, Rocky Mountain sheep, Ovis canadensis',
 'ibex, Capra ibex',
 'hartebeest',
 'impala, Aepyceros melampus',
 'gazelle',
 'Arabian camel, dromedary, Camelus dromedarius',
 'llama',
 'weasel',
 'mink',
 'polecat, fitch, foulmart, foumart, Mustela putorius',
 'black-footed ferret, ferret, Mustela nigripes',
 'otter',
 'skunk, polecat, wood pussy',
 'badger',
 'armadillo',
 'three-toed sloth, ai, Bradypus tridactylus',
 'orangutan, orang, orangutang, Pongo pygmaeus',
 'gorilla, Gorilla gorilla',
 'chimpanzee, chimp, Pan troglodytes',
 'gibbon, Hylobates lar',
 'siamang, Hylobates syndactylus, Symphalangus syndactylus',
 'guenon, guenon monkey',
 'patas, hussar monkey, Erythrocebus patas',
 'baboon',
 'macaque',
 'langur',
 'colobus, colobus monkey',
 'proboscis monkey, Nasalis larvatus',
 'marmoset',
 'capuchin, ringtail, Cebus capucinus',
 'howler monkey, howler',
 'titi, titi monkey',
 'spider monkey, Ateles geoffroyi',
 'squirrel monkey, Saimiri sciureus',
 'Madagascar cat, ring-tailed lemur, Lemur catta',
 'indri, indris, Indri indri, Indri brevicaudatus',
 'Indian elephant, Elephas maximus',
 'African elephant, Loxodonta africana',
 'lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens',
 'giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca',
 'barracouta, snoek',
 'eel',
 'coho, cohoe, coho salmon, blue jack, silver salmon, Oncorhynchus kisutch',
 'rock beauty, Holocanthus tricolor',
 'anemone fish',
 'sturgeon',
 'gar, garfish, garpike, billfish, Lepisosteus osseus',
 'lionfish',
 'puffer, pufferfish, blowfish, globefish',
 'abacus',
 'abaya',
 "academic gown, academic robe, judge's robe",
 'accordion, piano accordion, squeeze box',
 'acoustic guitar',
 'aircraft carrier, carrier, flattop, attack aircraft carrier',
 'airliner',
 'airship, dirigible',
 'altar',
 'ambulance',
 'amphibian, amphibious vehicle',
 'analog clock',
 'apiary, bee house',
 'apron',
 'ashcan, trash can, garbage can, wastebin, ash bin, ash-bin, ashbin, dustbin, trash barrel, trash bin',
 'assault rifle, assault gun',
 'backpack, back pack, knapsack, packsack, rucksack, haversack',
 'bakery, bakeshop, bakehouse',
 'balance beam, beam',
 'balloon',
 'ballpoint, ballpoint pen, ballpen, Biro',
 'Band Aid',
 'banjo',
 'bannister, banister, balustrade, balusters, handrail',
 'barbell',
 'barber chair',
 'barbershop',
 'barn',
 'barometer',
 'barrel, cask',
 'barrow, garden cart, lawn cart, wheelbarrow',
 'baseball',
 'basketball',
 'bassinet',
 'bassoon',
 'bathing cap, swimming cap',
 'bath towel',
 'bathtub, bathing tub, bath, tub',
 'beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon',
 'beacon, lighthouse, beacon light, pharos',
 'beaker',
 'bearskin, busby, shako',
 'beer bottle',
 'beer glass',
 'bell cote, bell cot',
 'bib',
 'bicycle-built-for-two, tandem bicycle, tandem',
 'bikini, two-piece',
 'binder, ring-binder',
 'binoculars, field glasses, opera glasses',
 'birdhouse',
 'boathouse',
 'bobsled, bobsleigh, bob',
 'bolo tie, bolo, bola tie, bola',
 'bonnet, poke bonnet',
 'bookcase',
 'bookshop, bookstore, bookstall',
 'bottlecap',
 'bow',
 'bow tie, bow-tie, bowtie',
 'brass, memorial tablet, plaque',
 'brassiere, bra, bandeau',
 'breakwater, groin, groyne, mole, bulwark, seawall, jetty',
 'breastplate, aegis, egis',
 'broom',
 'bucket, pail',
 'buckle',
 'bulletproof vest',
 'bullet train, bullet',
 'butcher shop, meat market',
 'cab, hack, taxi, taxicab',
 'caldron, cauldron',
 'candle, taper, wax light',
 'cannon',
 'canoe',
 'can opener, tin opener',
 'cardigan',
 'car mirror',
 'carousel, carrousel, merry-go-round, roundabout, whirligig',
 "carpenter's kit, tool kit",
 'carton',
 'car wheel',
 'cash machine, cash dispenser, automated teller machine, automatic teller machine, automated teller, automatic teller, ATM',
 'cassette',
 'cassette player',
 'castle',
 'catamaran',
 'CD player',
 'cello, violoncello',
 'cellular telephone, cellular phone, cellphone, cell, mobile phone',
 'chain',
 'chainlink fence',
 'chain mail, ring mail, mail, chain armor, chain armour, ring armor, ring armour',
 'chain saw, chainsaw',
 'chest',
 'chiffonier, commode',
 'chime, bell, gong',
 'china cabinet, china closet',
 'Christmas stocking',
 'church, church building',
 'cinema, movie theater, movie theatre, movie house, picture palace',
 'cleaver, meat cleaver, chopper',
 'cliff dwelling',
 'cloak',
 'clog, geta, patten, sabot',
 'cocktail shaker',
 'coffee mug',
 'coffeepot',
 'coil, spiral, volute, whorl, helix',
 'combination lock',
 'computer keyboard, keypad',
 'confectionery, confectionary, candy store',
 'container ship, containership, container vessel',
 'convertible',
 'corkscrew, bottle screw',
 'cornet, horn, trumpet, trump',
 'cowboy boot',
 'cowboy hat, ten-gallon hat',
 'cradle',
 'crane',
 'crash helmet',
 'crate',
 'crib, cot',
 'Crock Pot',
 'croquet ball',
 'crutch',
 'cuirass',
 'dam, dike, dyke',
 'desk',
 'desktop computer',
 'dial telephone, dial phone',
 'diaper, nappy, napkin',
 'digital clock',
 'digital watch',
 'dining table, board',
 'dishrag, dishcloth',
 'dishwasher, dish washer, dishwashing machine',
 'disk brake, disc brake',
 'dock, dockage, docking facility',
 'dogsled, dog sled, dog sleigh',
 'dome',
 'doormat, welcome mat',
 'drilling platform, offshore rig',
 'drum, membranophone, tympan',
 'drumstick',
 'dumbbell',
 'Dutch oven',
 'electric fan, blower',
 'electric guitar',
 'electric locomotive',
 'entertainment center',
 'envelope',
 'espresso maker',
 'face powder',
 'feather boa, boa',
 'file, file cabinet, filing cabinet',
 'fireboat',
 'fire engine, fire truck',
 'fire screen, fireguard',
 'flagpole, flagstaff',
 'flute, transverse flute',
 'folding chair',
 'football helmet',
 'forklift',
 'fountain',
 'fountain pen',
 'four-poster',
 'freight car',
 'French horn, horn',
 'frying pan, frypan, skillet',
 'fur coat',
 'garbage truck, dustcart',
 'gasmask, respirator, gas helmet',
 'gas pump, gasoline pump, petrol pump, island dispenser',
 'goblet',
 'go-kart',
 'golf ball',
 'golfcart, golf cart',
 'gondola',
 'gong, tam-tam',
 'gown',
 'grand piano, grand',
 'greenhouse, nursery, glasshouse',
 'grille, radiator grille',
 'grocery store, grocery, food market, market',
 'guillotine',
 'hair slide',
 'hair spray',
 'half track',
 'hammer',
 'hamper',
 'hand blower, blow dryer, blow drier, hair dryer, hair drier',
 'hand-held computer, hand-held microcomputer',
 'handkerchief, hankie, hanky, hankey',
 'hard disc, hard disk, fixed disk',
 'harmonica, mouth organ, harp, mouth harp',
 'harp',
 'harvester, reaper',
 'hatchet',
 'holster',
 'home theater, home theatre',
 'honeycomb',
 'hook, claw',
 'hoopskirt, crinoline',
 'horizontal bar, high bar',
 'horse cart, horse-cart',
 'hourglass',
 'iPod',
 'iron, smoothing iron',
 "jack-o'-lantern",
 'jean, blue jean, denim',
 'jeep, landrover',
 'jersey, T-shirt, tee shirt',
 'jigsaw puzzle',
 'jinrikisha, ricksha, rickshaw',
 'joystick',
 'kimono',
 'knee pad',
 'knot',
 'lab coat, laboratory coat',
 'ladle',
 'lampshade, lamp shade',
 'laptop, laptop computer',
 'lawn mower, mower',
 'lens cap, lens cover',
 'letter opener, paper knife, paperknife',
 'library',
 'lifeboat',
 'lighter, light, igniter, ignitor',
 'limousine, limo',
 'liner, ocean liner',
 'lipstick, lip rouge',
 'Loafer',
 'lotion',
 'loudspeaker, speaker, speaker unit, loudspeaker system, speaker system',
 "loupe, jeweler's loupe",
 'lumbermill, sawmill',
 'magnetic compass',
 'mailbag, postbag',
 'mailbox, letter box',
 'maillot',
 'maillot, tank suit',
 'manhole cover',
 'maraca',
 'marimba, xylophone',
 'mask',
 'matchstick',
 'maypole',
 'maze, labyrinth',
 'measuring cup',
 'medicine chest, medicine cabinet',
 'megalith, megalithic structure',
 'microphone, mike',
 'microwave, microwave oven',
 'military uniform',
 'milk can',
 'minibus',
 'miniskirt, mini',
 'minivan',
 'missile',
 'mitten',
 'mixing bowl',
 'mobile home, manufactured home',
 'Model T',
 'modem',
 'monastery',
 'monitor',
 'moped',
 'mortar',
 'mortarboard',
 'mosque',
 'mosquito net',
 'motor scooter, scooter',
 'mountain bike, all-terrain bike, off-roader',
 'mountain tent',
 'mouse, computer mouse',
 'mousetrap',
 'moving van',
 'muzzle',
 'nail',
 'neck brace',
 'necklace',
 'nipple',
 'notebook, notebook computer',
 'obelisk',
 'oboe, hautboy, hautbois',
 'ocarina, sweet potato',
 'odometer, hodometer, mileometer, milometer',
 'oil filter',
 'organ, pipe organ',
 'oscilloscope, scope, cathode-ray oscilloscope, CRO',
 'overskirt',
 'oxcart',
 'oxygen mask',
 'packet',
 'paddle, boat paddle',
 'paddlewheel, paddle wheel',
 'padlock',
 'paintbrush',
 "pajama, pyjama, pj's, jammies",
 'palace',
 'panpipe, pandean pipe, syrinx',
 'paper towel',
 'parachute, chute',
 'parallel bars, bars',
 'park bench',
 'parking meter',
 'passenger car, coach, carriage',
 'patio, terrace',
 'pay-phone, pay-station',
 'pedestal, plinth, footstall',
 'pencil box, pencil case',
 'pencil sharpener',
 'perfume, essence',
 'Petri dish',
 'photocopier',
 'pick, plectrum, plectron',
 'pickelhaube',
 'picket fence, paling',
 'pickup, pickup truck',
 'pier',
 'piggy bank, penny bank',
 'pill bottle',
 'pillow',
 'ping-pong ball',
 'pinwheel',
 'pirate, pirate ship',
 'pitcher, ewer',
 "plane, carpenter's plane, woodworking plane",
 'planetarium',
 'plastic bag',
 'plate rack',
 'plow, plough',
 "plunger, plumber's helper",
 'Polaroid camera, Polaroid Land camera',
 'pole',
 'police van, police wagon, paddy wagon, patrol wagon, wagon, black Maria',
 'poncho',
 'pool table, billiard table, snooker table',
 'pop bottle, soda bottle',
 'pot, flowerpot',
 "potter's wheel",
 'power drill',
 'prayer rug, prayer mat',
 'printer',
 'prison, prison house',
 'projectile, missile',
 'projector',
 'puck, hockey puck',
 'punching bag, punch bag, punching ball, punchball',
 'purse',
 'quill, quill pen',
 'quilt, comforter, comfort, puff',
 'racer, race car, racing car',
 'racket, racquet',
 'radiator',
 'radio, wireless',
 'radio telescope, radio reflector',
 'rain barrel',
 'recreational vehicle, RV, R.V.',
 'reel',
 'reflex camera',
 'refrigerator, icebox',
 'remote control, remote',
 'restaurant, eating house, eating place, eatery',
 'revolver, six-gun, six-shooter',
 'rifle',
 'rocking chair, rocker',
 'rotisserie',
 'rubber eraser, rubber, pencil eraser',
 'rugby ball',
 'rule, ruler',
 'running shoe',
 'safe',
 'safety pin',
 'saltshaker, salt shaker',
 'sandal',
 'sarong',
 'sax, saxophone',
 'scabbard',
 'scale, weighing machine',
 'school bus',
 'schooner',
 'scoreboard',
 'screen, CRT screen',
 'screw',
 'screwdriver',
 'seat belt, seatbelt',
 'sewing machine',
 'shield, buckler',
 'shoe shop, shoe-shop, shoe store',
 'shoji',
 'shopping basket',
 'shopping cart',
 'shovel',
 'shower cap',
 'shower curtain',
 'ski',
 'ski mask',
 'sleeping bag',
 'slide rule, slipstick',
 'sliding door',
 'slot, one-armed bandit',
 'snorkel',
 'snowmobile',
 'snowplow, snowplough',
 'soap dispenser',
 'soccer ball',
 'sock',
 'solar dish, solar collector, solar furnace',
 'sombrero',
 'soup bowl',
 'space bar',
 'space heater',
 'space shuttle',
 'spatula',
 'speedboat',
 "spider web, spider's web",
 'spindle',
 'sports car, sport car',
 'spotlight, spot',
 'stage',
 'steam locomotive',
 'steel arch bridge',
 'steel drum',
 'stethoscope',
 'stole',
 'stone wall',
 'stopwatch, stop watch',
 'stove',
 'strainer',
 'streetcar, tram, tramcar, trolley, trolley car',
 'stretcher',
 'studio couch, day bed',
 'stupa, tope',
 'submarine, pigboat, sub, U-boat',
 'suit, suit of clothes',
 'sundial',
 'sunglass',
 'sunglasses, dark glasses, shades',
 'sunscreen, sunblock, sun blocker',
 'suspension bridge',
 'swab, swob, mop',
 'sweatshirt',
 'swimming trunks, bathing trunks',
 'swing',
 'switch, electric switch, electrical switch',
 'syringe',
 'table lamp',
 'tank, army tank, armored combat vehicle, armoured combat vehicle',
 'tape player',
 'teapot',
 'teddy, teddy bear',
 'television, television system',
 'tennis ball',
 'thatch, thatched roof',
 'theater curtain, theatre curtain',
 'thimble',
 'thresher, thrasher, threshing machine',
 'throne',
 'tile roof',
 'toaster',
 'tobacco shop, tobacconist shop, tobacconist',
 'toilet seat',
 'torch',
 'totem pole',
 'tow truck, tow car, wrecker',
 'toyshop',
 'tractor',
 'trailer truck, tractor trailer, trucking rig, rig, articulated lorry, semi',
 'tray',
 'trench coat',
 'tricycle, trike, velocipede',
 'trimaran',
 'tripod',
 'triumphal arch',
 'trolleybus, trolley coach, trackless trolley',
 'trombone',
 'tub, vat',
 'turnstile',
 'typewriter keyboard',
 'umbrella',
 'unicycle, monocycle',
 'upright, upright piano',
 'vacuum, vacuum cleaner',
 'vase',
 'vault',
 'velvet',
 'vending machine',
 'vestment',
 'viaduct',
 'violin, fiddle',
 'volleyball',
 'waffle iron',
 'wall clock',
 'wallet, billfold, notecase, pocketbook',
 'wardrobe, closet, press',
 'warplane, military plane',
 'washbasin, handbasin, washbowl, lavabo, wash-hand basin',
 'washer, automatic washer, washing machine',
 'water bottle',
 'water jug',
 'water tower',
 'whiskey jug',
 'whistle',
 'wig',
 'window screen',
 'window shade',
 'Windsor tie',
 'wine bottle',
 'wing',
 'wok',
 'wooden spoon',
 'wool, woolen, woollen',
 'worm fence, snake fence, snake-rail fence, Virginia fence',
 'wreck',
 'yawl',
 'yurt',
 'web site, website, internet site, site',
 'comic book',
 'crossword puzzle, crossword',
 'street sign',
 'traffic light, traffic signal, stoplight',
 'book jacket, dust cover, dust jacket, dust wrapper',
 'menu',
 'plate',
 'guacamole',
 'consomme',
 'hot pot, hotpot',
 'trifle',
 'ice cream, icecream',
 'ice lolly, lolly, lollipop, popsicle',
 'French loaf',
 'bagel, beigel',
 'pretzel',
 'cheeseburger',
 'hotdog, hot dog, red hot',
 'mashed potato',
 'head cabbage',
 'broccoli',
 'cauliflower',
 'zucchini, courgette',
 'spaghetti squash',
 'acorn squash',
 'butternut squash',
 'cucumber, cuke',
 'artichoke, globe artichoke',
 'bell pepper',
 'cardoon',
 'mushroom',
 'Granny Smith',
 'strawberry',
 'orange',
 'lemon',
 'fig',
 'pineapple, ananas',
 'banana',
 'jackfruit, jak, jack',
 'custard apple',
 'pomegranate',
 'hay',
 'carbonara',
 'chocolate sauce, chocolate syrup',
 'dough',
 'meat loaf, meatloaf',
 'pizza, pizza pie',
 'potpie',
 'burrito',
 'red wine',
 'espresso',
 'cup',
 'eggnog',
 'alp',
 'bubble',
 'cliff, drop, drop-off',
 'coral reef',
 'geyser',
 'lakeside, lakeshore',
 'promontory, headland, head, foreland',
 'sandbar, sand bar',
 'seashore, coast, seacoast, sea-coast',
 'valley, vale',
 'volcano',
 'ballplayer, baseball player',
 'groom, bridegroom',
 'scuba diver',
 'rapeseed',
 'daisy',
 "yellow lady's slipper, yellow lady-slipper, Cypripedium calceolus, Cypripedium parviflorum",
 'corn',
 'acorn',
 'hip, rose hip, rosehip',
 'buckeye, horse chestnut, conker',
 'coral fungus',
 'agaric',
 'gyromitra',
 'stinkhorn, carrion fungus',
 'earthstar',
 'hen-of-the-woods, hen of the woods, Polyporus frondosus, Grifola frondosa',
 'bolete',
 'ear, spike, capitulum',
 'toilet tissue, toilet paper, bathroom tissue']
In [18]:
# Determine the index corresponding to the maximum score in the out tensor
_, index = torch.max(out, 1)
index
Out[18]:
tensor([207])
In [19]:
# Normalize our outputs to the range [0, 1]
percentage = torch.nn.functional.softmax(out, dim=1)[0] * 100
labels[index[0]], percentage[index[0]].item()
Out[19]:
('golden retriever', 96.57185363769531)
In [20]:
# Determine the top five indices corresponding to the top best scores in the out tensor
_, indices = torch.sort(out, descending=True)
[(labels[idx], percentage[idx].item()) for idx in indices[0][:5]]
Out[20]:
[('golden retriever', 96.57185363769531),
 ('Labrador retriever', 2.6082630157470703),
 ('cocker spaniel, English cocker spaniel, cocker', 0.26996269822120667),
 ('redbone', 0.17958903312683105),
 ('tennis ball', 0.10991978645324707)]

6. CycleGAN¶

A CycleGAN can turn images of one domain into images of another domain (and back), without the need for us to explicitly provide matching pairs in the training set. The CycleGAN network has been trained on a dataset of (unrelated) horse images and zebra images extracted from the ImageNet dataset.

In [21]:
class ResNetBlock(nn.Module): # <1>

    def __init__(self, dim):
        super(ResNetBlock, self).__init__()
        self.conv_block = self.build_conv_block(dim)

    def build_conv_block(self, dim):
        conv_block = []

        conv_block += [nn.ReflectionPad2d(1)]

        conv_block += [nn.Conv2d(dim, dim, kernel_size=3, padding=0, bias=True),
                       nn.InstanceNorm2d(dim),
                       nn.ReLU(True)]

        conv_block += [nn.ReflectionPad2d(1)]

        conv_block += [nn.Conv2d(dim, dim, kernel_size=3, padding=0, bias=True),
                       nn.InstanceNorm2d(dim)]

        return nn.Sequential(*conv_block)

    def forward(self, x):
        out = x + self.conv_block(x) # <2>
        return out


class ResNetGenerator(nn.Module):

    def __init__(self, input_nc=3, output_nc=3, ngf=64, n_blocks=9): # <3>

        assert(n_blocks >= 0)
        super(ResNetGenerator, self).__init__()

        self.input_nc = input_nc
        self.output_nc = output_nc
        self.ngf = ngf

        model = [nn.ReflectionPad2d(3),
                 nn.Conv2d(input_nc, ngf, kernel_size=7, padding=0, bias=True),
                 nn.InstanceNorm2d(ngf),
                 nn.ReLU(True)]

        n_downsampling = 2
        for i in range(n_downsampling):
            mult = 2**i
            model += [nn.Conv2d(ngf * mult, ngf * mult * 2, kernel_size=3,
                                stride=2, padding=1, bias=True),
                      nn.InstanceNorm2d(ngf * mult * 2),
                      nn.ReLU(True)]

        mult = 2**n_downsampling
        for i in range(n_blocks):
            model += [ResNetBlock(ngf * mult)]

        for i in range(n_downsampling):
            mult = 2**(n_downsampling - i)
            model += [nn.ConvTranspose2d(ngf * mult, int(ngf * mult / 2),
                                         kernel_size=3, stride=2,
                                         padding=1, output_padding=1,
                                         bias=True),
                      nn.InstanceNorm2d(int(ngf * mult / 2)),
                      nn.ReLU(True)]

        model += [nn.ReflectionPad2d(3)]
        model += [nn.Conv2d(ngf, output_nc, kernel_size=7, padding=0)]
        model += [nn.Tanh()]

        self.model = nn.Sequential(*model)

    def forward(self, input): # <3>
        return self.model(input)
In [22]:
# Define a ResNetGenerator class
netG = ResNetGenerator()

We mentioned earlier that we would run a generator model that had been pretrained on the horse2zebra dataset, whose training set contains two sets of 1068 and 1335 images of horses and zebras, respectively. The dataset be found at http://mng.bz/8pKP. The weights of the model have been saved in a .pth file, which is nothing but a pickle file of the model’s tensor parameters. We can load those into ResNetGenerator using the model’s load _state_dict method:

In [23]:
# Load the weights
model_path = '/content/drive/MyDrive/IS675_data/horse2zebra_0.4.0.pth'
model_data = torch.load(model_path)
netG.load_state_dict(model_data)
<ipython-input-23-5021a5198034>:3: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
  model_data = torch.load(model_path)
Out[23]:
<All keys matched successfully>

This is fully equivalent to what happened when we loaded resnet101 from torchvision.

In [24]:
# Put the model in eval mode
netG.eval()
Out[24]:
ResNetGenerator(
  (model): Sequential(
    (0): ReflectionPad2d((3, 3, 3, 3))
    (1): Conv2d(3, 64, kernel_size=(7, 7), stride=(1, 1))
    (2): InstanceNorm2d(64, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
    (3): ReLU(inplace=True)
    (4): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
    (5): InstanceNorm2d(128, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
    (6): ReLU(inplace=True)
    (7): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
    (8): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
    (9): ReLU(inplace=True)
    (10): ResNetBlock(
      (conv_block): Sequential(
        (0): ReflectionPad2d((1, 1, 1, 1))
        (1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
        (2): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
        (3): ReLU(inplace=True)
        (4): ReflectionPad2d((1, 1, 1, 1))
        (5): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
        (6): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
      )
    )
    (11): ResNetBlock(
      (conv_block): Sequential(
        (0): ReflectionPad2d((1, 1, 1, 1))
        (1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
        (2): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
        (3): ReLU(inplace=True)
        (4): ReflectionPad2d((1, 1, 1, 1))
        (5): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
        (6): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
      )
    )
    (12): ResNetBlock(
      (conv_block): Sequential(
        (0): ReflectionPad2d((1, 1, 1, 1))
        (1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
        (2): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
        (3): ReLU(inplace=True)
        (4): ReflectionPad2d((1, 1, 1, 1))
        (5): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
        (6): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
      )
    )
    (13): ResNetBlock(
      (conv_block): Sequential(
        (0): ReflectionPad2d((1, 1, 1, 1))
        (1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
        (2): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
        (3): ReLU(inplace=True)
        (4): ReflectionPad2d((1, 1, 1, 1))
        (5): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
        (6): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
      )
    )
    (14): ResNetBlock(
      (conv_block): Sequential(
        (0): ReflectionPad2d((1, 1, 1, 1))
        (1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
        (2): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
        (3): ReLU(inplace=True)
        (4): ReflectionPad2d((1, 1, 1, 1))
        (5): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
        (6): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
      )
    )
    (15): ResNetBlock(
      (conv_block): Sequential(
        (0): ReflectionPad2d((1, 1, 1, 1))
        (1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
        (2): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
        (3): ReLU(inplace=True)
        (4): ReflectionPad2d((1, 1, 1, 1))
        (5): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
        (6): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
      )
    )
    (16): ResNetBlock(
      (conv_block): Sequential(
        (0): ReflectionPad2d((1, 1, 1, 1))
        (1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
        (2): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
        (3): ReLU(inplace=True)
        (4): ReflectionPad2d((1, 1, 1, 1))
        (5): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
        (6): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
      )
    )
    (17): ResNetBlock(
      (conv_block): Sequential(
        (0): ReflectionPad2d((1, 1, 1, 1))
        (1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
        (2): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
        (3): ReLU(inplace=True)
        (4): ReflectionPad2d((1, 1, 1, 1))
        (5): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
        (6): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
      )
    )
    (18): ResNetBlock(
      (conv_block): Sequential(
        (0): ReflectionPad2d((1, 1, 1, 1))
        (1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
        (2): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
        (3): ReLU(inplace=True)
        (4): ReflectionPad2d((1, 1, 1, 1))
        (5): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1))
        (6): InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
      )
    )
    (19): ConvTranspose2d(256, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), output_padding=(1, 1))
    (20): InstanceNorm2d(128, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
    (21): ReLU(inplace=True)
    (22): ConvTranspose2d(128, 64, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), output_padding=(1, 1))
    (23): InstanceNorm2d(64, eps=1e-05, momentum=0.1, affine=False, track_running_stats=False)
    (24): ReLU(inplace=True)
    (25): ReflectionPad2d((3, 3, 3, 3))
    (26): Conv2d(64, 3, kernel_size=(7, 7), stride=(1, 1))
    (27): Tanh()
  )
)

It takes an image, recognizes one or more horses in it by looking at pixels, and individually modifies the values of those pixels so that what comes out looks like a credible zebra. We won’t recognize anything zebra-like in the printout (or in the source code, for that matter): that’s because there’s nothing zebralike in there. The network is a scaffold—the juice is in the weights.

In [25]:
# Load a random image of a horse
img = Image.open("/content/drive/MyDrive/IS675_data/horse.jpg")
img
Out[25]:
In [26]:
preprocess = transforms.Compose([transforms.Resize(256),
                                transforms.ToTensor()])
In [27]:
# Preprocess the image
img_t = preprocess(img)
In [28]:
# Adding a dimension to our image to make it a batch of one image!
batch_t = torch.unsqueeze(img_t, 0)
batch_t
Out[28]:
tensor([[[[0.6902, 0.6902, 0.6902,  ..., 0.6902, 0.6902, 0.6902],
          [0.6863, 0.6863, 0.6863,  ..., 0.6902, 0.6902, 0.6863],
          [0.6902, 0.6863, 0.6902,  ..., 0.6902, 0.6902, 0.6902],
          ...,
          [0.7098, 0.6980, 0.6745,  ..., 0.7412, 0.7020, 0.7098],
          [0.6980, 0.6902, 0.6157,  ..., 0.7451, 0.7255, 0.7176],
          [0.5725, 0.5765, 0.5882,  ..., 0.5961, 0.6392, 0.7059]],

         [[0.7255, 0.7255, 0.7255,  ..., 0.7255, 0.7255, 0.7255],
          [0.7216, 0.7216, 0.7216,  ..., 0.7255, 0.7255, 0.7216],
          [0.7255, 0.7216, 0.7255,  ..., 0.7255, 0.7255, 0.7255],
          ...,
          [0.6235, 0.6078, 0.5882,  ..., 0.6392, 0.6000, 0.6078],
          [0.6078, 0.6000, 0.5333,  ..., 0.6431, 0.6235, 0.6157],
          [0.4902, 0.4941, 0.4980,  ..., 0.4902, 0.5333, 0.6039]],

         [[0.9569, 0.9569, 0.9569,  ..., 0.9569, 0.9569, 0.9569],
          [0.9529, 0.9529, 0.9529,  ..., 0.9569, 0.9569, 0.9529],
          [0.9569, 0.9529, 0.9569,  ..., 0.9569, 0.9569, 0.9569],
          ...,
          [0.5294, 0.5137, 0.4980,  ..., 0.5373, 0.5059, 0.5098],
          [0.5137, 0.5098, 0.4392,  ..., 0.5490, 0.5255, 0.5137],
          [0.4118, 0.4235, 0.4118,  ..., 0.3961, 0.4275, 0.4902]]]])
In [29]:
# Inference
batch_out = netG(batch_t)
In [32]:
# convert batch_out back to an image
out_t = (batch_out.data.squeeze() + 1.0) / 2.0
out_img = transforms.ToPILImage()(out_t)
out_img.save('/content/drive/MyDrive/IS675_data/zebra.jpg')
out_img
Out[32]:

Q1- Turn bobby into a zebra!

Q2- Classify the horse image.

Q3- Turn a random group of horses into zebras.

Q4- Classify a random picture and try to fool the model (I am looking for misclassification instances where the model is totally off in its prediction wrt top five choices)

In [35]:
img = Image.open("/content/drive/MyDrive/IS675_data/bobby.jpg")
img
Out[35]:
In [36]:
preprocess = transforms.Compose([transforms.Resize(256),
                                transforms.ToTensor()])
In [37]:
img_t = preprocess(img)
In [38]:
batch_t = torch.unsqueeze(img_t, 0)
batch_t
Out[38]:
tensor([[[[0.2980, 0.3059, 0.3020,  ..., 0.4549, 0.4627, 0.4588],
          [0.2980, 0.3059, 0.3059,  ..., 0.4588, 0.4627, 0.4549],
          [0.3137, 0.3176, 0.3098,  ..., 0.4549, 0.4588, 0.4549],
          ...,
          [0.8471, 0.8431, 0.8392,  ..., 0.6824, 0.6980, 0.7020],
          [0.8471, 0.8431, 0.8392,  ..., 0.6784, 0.6863, 0.6980],
          [0.8471, 0.8431, 0.8392,  ..., 0.6471, 0.6588, 0.6667]],

         [[0.1686, 0.1765, 0.1725,  ..., 0.3137, 0.3137, 0.3059],
          [0.1529, 0.1647, 0.1647,  ..., 0.3216, 0.3216, 0.3137],
          [0.1569, 0.1647, 0.1569,  ..., 0.3255, 0.3255, 0.3216],
          ...,
          [0.6510, 0.6471, 0.6431,  ..., 0.4627, 0.4706, 0.4745],
          [0.6510, 0.6471, 0.6431,  ..., 0.4706, 0.4745, 0.4863],
          [0.6510, 0.6471, 0.6431,  ..., 0.4471, 0.4588, 0.4667]],

         [[0.0745, 0.0706, 0.0510,  ..., 0.1843, 0.2000, 0.2000],
          [0.0588, 0.0588, 0.0510,  ..., 0.1804, 0.1922, 0.1882],
          [0.0627, 0.0627, 0.0471,  ..., 0.1725, 0.1804, 0.1765],
          ...,
          [0.3098, 0.3059, 0.3020,  ..., 0.1686, 0.1922, 0.2078],
          [0.3098, 0.3059, 0.3020,  ..., 0.1765, 0.1922, 0.2078],
          [0.3098, 0.3059, 0.3020,  ..., 0.1686, 0.1882, 0.1961]]]])
In [39]:
# Inference
batch_out = netG(batch_t)
In [40]:
out_t = (batch_out.data.squeeze() + 1.0) / 2.0
out_img = transforms.ToPILImage()(out_t)
#out_img.save('/content/drive/MyDrive/IS675_data/zebra.jpg')
out_img
Out[40]:
In [44]:
#Classify the horse image.
img = Image.open("/content/drive/MyDrive/IS675_data/testhorse.jpg")
img
Out[44]:
In [45]:
img_t = preprocess(img)
batch_t = torch.unsqueeze(img_t, 0)
resnet.eval()

out = resnet(batch_t)
with open('/content/drive/MyDrive/IS675_data/imagenet_classes.txt') as f:
  labels = [line.strip() for line in f.readlines()]

_, index = torch.max(out, 1)
index

percentage = torch.nn.functional.softmax(out, dim=1)[0] * 100
labels[index[0]], percentage[index[0]].item()

_, indices = torch.sort(out, descending=True)
[(labels[idx], percentage[idx].item()) for idx in indices[0][:5]]
Out[45]:
[('sorrel', 23.316713333129883),
 ('horse cart, horse-cart', 21.583011627197266),
 ('Arabian camel, dromedary, Camelus dromedarius', 11.088310241699219),
 ('plow, plough', 8.102662086486816),
 ('ox', 4.294273853302002)]
In [46]:
#Turn a random group of horses into zebras.
img2 = Image.open("/content/drive/MyDrive/IS675_data/ghorse.jpg")

# img2
img2_t = preprocess(img2)
batch2_t = torch.unsqueeze(img2_t, 0)
batch_out = netG(batch2_t)

# convert batch_out back to an image
out_t = (batch_out.data.squeeze() + 1.0) / 2.0
out_img = transforms.ToPILImage()(out_t)
#out_img.save('/content/drive/MyDrive/Colab Notebooks/IS675_data_Lab3/gzebrahorse.jpg')
out_img
Out[46]:
In [57]:
img_2 = Image.open("/content/drive/MyDrive/IS675_data/io.jpeg")
img_2
Out[57]:
In [58]:
img_t = preprocess(img_2)
batch_t = torch.unsqueeze(img_t, 0)
resnet.eval()

out = resnet(batch_t)
with open('/content/drive/MyDrive/IS675_data/imagenet_classes.txt') as f:
  labels = [line.strip() for line in f.readlines()]

_, index = torch.max(out, 1)
index

percentage = torch.nn.functional.softmax(out, dim=1)[0] * 100
labels[index[0]], percentage[index[0]].item()

_, indices = torch.sort(out, descending=True)
[(labels[idx], percentage[idx].item()) for idx in indices[0][:5]]
Out[58]:
[('ox', 62.90299987792969),
 ('hartebeest', 5.38269567489624),
 ('water buffalo, water ox, Asiatic buffalo, Bubalus bubalis',
  4.655623435974121),
 ('oxcart', 2.2137610912323),
 ('Doberman, Doberman pinscher', 2.1414284706115723)]
In [ ]:
!jupyter nbconvert --to html ""